package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.Employee;
import cn.wolfcode.cms.query.EmployeeQueryObject;
import cn.wolfcode.cms.service.IEmployeeService;
import cn.wolfcode.cms.util.JSONResult;
import cn.wolfcode.cms.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;


    //员工首页
    @RequiresPermissions("employee:index")
    @PermissionName("员工首页")
    @RequestMapping("")
    public String index(){
        return "employee/list";
    }


    //员工数据列表
    @RequestMapping("list")
    @RequiresPermissions("employee:list")
    @PermissionName("员工列表")
    @ResponseBody
    public Object list(EmployeeQueryObject qo){

        //需求: 模拟演示不同角色可以查询不同数据, 如果拥有HR_MGR可以查询所有, 反之只能查询自己
        //判断当前用户是否拥有HR_MGR角色
        if (!SecurityUtils.getSubject().hasRole("HR_MGR")) {
            //获取当前登录用户
            Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
            //select * from employee where id = #{currentUserId}
            qo.setCurrentUserId(currentUser.getId());
        }


        PageResult result = employeeService.query(qo);
        //{"total":10, "rows":[]}
        return result;
    }


    //员工添加
    @RequiresPermissions("employee:save")
    @PermissionName("员工添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(Employee entity){
        JSONResult result = new JSONResult();
        try {
            employeeService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result ;
    }

    //员工编辑
    @RequiresPermissions("employee:edit")
    @PermissionName("员工编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(Employee entity){
        JSONResult result = new JSONResult();
        try {
            employeeService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result ;
    }


    //员工删除
    @RequiresPermissions("employee:delete")
    @PermissionName("员工删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){
        JSONResult result = new JSONResult();
        try {
            employeeService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result ;
    }

    //员工离职/复职
    @RequiresPermissions("employee:quit")
    @PermissionName("员工离职")
    @RequestMapping("quit")
    @ResponseBody
    public Object quit(Long id){
        JSONResult result = new JSONResult();
        try {
            employeeService.quit(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result ;
    }

}
